<?php
namespace app\shawting\controller;
use think\Validate;

class Advertising extends Base
{
    public function index(){
        $search = input('search')?input('search'):"";
        $where["A.publish_id"] = ["like","%{$search}%"];
        $arr = db("city_advertising")->alias("A")
            ->field("A.*,B.space_name,B.serial_number,C.nick_name,UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(now()) as time_difference")
            ->join("city_advertising_space B","A.space_id = B.id","left")
            ->join("city_user C","A.user_id = C.user_id","inner")
            ->where($where)
            ->where(["state"=>1])
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $this->assign("arr",$arr);
        $this->assign("search",$search);
        return $this->fetch();
    }

    public function ajax_delete(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error('查询数据格式不正确');
        }
        $re = db("city_advertising")->where(["id"=>$id])->update(["state"=>0]);
        if(!$re){
            return $this->error("删除失败");
        }else{
            return $this->success("删除成功");
        }
    }

    public function booth(){
        $arr = db("city_advertising_space")->alias("A")
            ->field("A.*,count(B.space_id) as count")
            ->join("city_advertising B","A.id = B.space_id and B.state=1","left")
            ->group("A.id,B.space_id")
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $sum_space = db("city_advertising_space")->sum('total_space');
        $sum = db("city_advertising")->where(["state"=>1])->count('id');
        $remain = $sum_space-$sum;
        $this->assign("arr",$arr);
        $this->assign("sum_space",$sum_space);
        $this->assign("sum",$sum);
        $this->assign("remain",$remain);
        return $this->fetch();
    }

    public function ajax_show_flag(){
        $id = input("id");
        $show_flag = input("show_flag");
        if(!is_numeric($id) && !is_numeric($show_flag)){
            return $this->error('查询数据格式不正确');
        }
        $data = db("city_advertising_space")->where(["id"=>$id])->update(["show_flag"=>$show_flag]);
        if(!$data){
            $this->error("查询数据失败！");
        }
        $this->success("操作成功");
    }

    public function ajax_find(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error('查询数据格式不正确');
        }
        $data = db("city_advertising_space")->where(["id"=>$id])->find();
        if(!$data){
            $this->error("查询数据失败！");
        }
        $this->success(null,null,$data);
    }

    public function ajax_edit_submit(){
        $data = input();
        $id = $data["id"];
        if(!is_numeric($id)){
            return $this->error('请求错误，请联系管理员');
        }
        $datas = [
            "space_name" => $data['space_name'],
            "serial_number" => $data['serial_number'],
            "month_cost" => $data['month_cost'],
            "season_cost" => $data['season_cost'],
            "half_year_cost" => $data['half_year_cost'],
            "year_cost" => $data['year_cost'],
        ];
        $validate = new Validate([
            "space_name" => "require",
            "serial_number" => "require",
            "month_cost" => "require",
            "season_cost" => "require",
            "half_year_cost" => "require",
            "year_cost" => "require",

        ],
            [
                "space_name.require" => "位置名称不能为空",
                "serial_number.require" => "编号不能为空",
                "month_cost.require" => "月费用不能为空",
                "season_cost.require" => "季费用不能为空",
                "half_year_cost.require" => "半年费用不能为空",
                "year_cost.require" => "年费用不能为空",
            ]
        );
        if(!$validate->check($datas)){
            $this->error($validate->getError());
        }
        $re = db("city_advertising_space")->where(["id"=>$id])->update($datas);
        if($re){
            $this->success("添加成功");
        }else{
            $this->error('添加失败');
        }
    }
}